package com.haoshuang.zhouzhoubang.ui.activity

import android.os.Bundle

import androidx.recyclerview.widget.LinearLayoutManager
import com.google.gson.Gson
import com.gyf.immersionbar.ktx.immersionBar
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.databinding.ActivitySelectSkillsBinding
import com.haoshuang.zhouzhoubang.model.bean.SortBean
import com.haoshuang.zhouzhoubang.ui.adapter.SkillLeftAdapter
import com.haoshuang.zhouzhoubang.ui.adapter.SkillRightAdapter
import com.haoshuang.zhouzhoubang.ui.vm.SelectSkillsViewModel3
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.clickWithTrigger


/**
 *  技能中心
 */
class SelectSkillsActivity3 :
    BaseVMActivity<SelectSkillsViewModel3, ActivitySelectSkillsBinding>() {

    override fun initView(savedInstanceState: Bundle?) {
        binding.apply {
            appBar.ivBack.clickWithTrigger { finish() }
            appBar.tvTitle.text = "技能中心"
        }
        immersionBar {
            statusBarDarkFont(false) //状态栏字体 true为深色 false为亮色
            statusBarColor(R.color.c_1b2834)   //设置状态栏颜色
            titleBarMarginTop(binding.root)
        }
    }

    var list = arrayListOf<SortBean>()
    var list2 = arrayListOf<SortBean.Data>()

    lateinit var adapter: SkillLeftAdapter
    lateinit var adapter2: SkillRightAdapter
    var listStr = arrayListOf<SortBean.Data.Data1>()
    var gson = Gson()

    var service_ids = arrayListOf<Int>()
    var small_ids = arrayListOf<Int>()
    var little_ids = ""
    var team_num = 0
    var cate_id = 0
    var tcate_id = 0
    var fcate_id = 0
    override fun initData() {
        cate_id = intent.getIntExtra("cate_id", 0)
        tcate_id = intent.getIntExtra("tcate_id", 0)
        fcate_id = intent.getIntExtra("fcate_id", 0)
        binding.apply {
            rvList.layoutManager = LinearLayoutManager(this@SelectSkillsActivity3)
            rvList2.layoutManager = LinearLayoutManager(this@SelectSkillsActivity3)
            adapter = SkillLeftAdapter(list)
            adapter2 = SkillRightAdapter(list2)
            rvList.adapter = adapter
            rvList2.adapter = adapter2
            llNext.setOnClickListener {
                listStr.clear()

                service_ids.clear()
                small_ids.clear()
                little_ids = ""
                team_num = 0
                var service_ids2 = 0
                var small_ids2 = 0
                for (b in list) {
                    service_ids2 = b.id
                    for (b1 in b.data_list) {
                        small_ids2 = b1.id
                        for (b2 in b1.data_list) {
                            if (b2.isCheck == 1) {
                                little_ids = little_ids + b2.id.toString() + ","
                                listStr.add(b2)
                                service_ids.add(service_ids2)
                                small_ids.add(small_ids2)
                            }
                        }
                    }
                }
                if (listStr.size > 0) {
                    var service_ids3 = ""
                    var small_ids3 = ""
                    var arr1 = service_ids.toSortedSet().toList()
                    var arr2 = small_ids.toSortedSet().toList()
                    for (b in list) {
                        for (b1 in service_ids) {
                            if (b.id == b1) {
                                team_num = b.team_num + team_num
                            }
                        }
                    }
                    for (b in arr1) {
                        service_ids3 = service_ids3 + b.toString() + ","
                    }
                    for (b in arr2) {
                        small_ids3 = small_ids3 + b.toString() + ","
                    }
                    vm.getData(
                        "service_ids" to service_ids3.substring(0, service_ids3.length - 1),
                        "small_ids" to small_ids3.substring(0, small_ids3.length - 1),
                        "little_ids" to little_ids.substring(0, little_ids.length - 1)
                    )
                } else {
                    toast("请选择技能")
                }


            }
            adapter.setOnItemClickListener { adapter, view, position ->
                if (list[position].isCheck == 0) {
                    for (b in list) {
                        b.isCheck = 0
                    }
                    list2.clear()
                    list2.addAll(list[position].data_list)
                    list[position].isCheck = 1
                    adapter.notifyDataSetChanged()
                    adapter2.notifyDataSetChanged()

                    //  ( rvList2.getLayoutManager() as LinearLayoutManager).scrollToPositionWithOffset(position, 0)
                }
            }
        }

        vm.getJn()
    }

    override fun startObserve() {
        vm.getData.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                if (code!="402"){
                    toast(msg)
                }
            }
            onSuccess {
                dismissLoading()
                toast("提交成功")
                finish()
            }
        }
        vm.getJn.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                if (code!="402"){
                    toast(msg)
                }
            }
            onSuccess {
                dismissLoading()
                list.addAll(it!!)
                if (list.size > 0) {
                    for (b in list) {
                        if (b.id == fcate_id) {
                            b.isCheck = 1
                            list2.clear()
                            list2.addAll(b.data_list)
                            for (k in list2){
                                if (k.id==cate_id){
                                    for (j in k.data_list){
                                        if (j.id==tcate_id){
                                            j.isCheck=1
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                adapter.notifyDataSetChanged()
                adapter2.notifyDataSetChanged()
            }
        }

    }

}